package com.jz.community.moduleauthorization.utils;

import android.app.Activity;
import android.content.Context;

import com.jz.community.basecomm.base.BaseApplication;
import com.jz.community.basecomm.constant.RequseResultConstants;
import com.jz.community.basecomm.routerUtils.RouterCommonUtils;
import com.jz.community.basecomm.routerUtils.RouterIntentConstant;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.rxbus.RxBus;
import com.jz.community.basecomm.utils.rxbus.eventType.ToLoginEvent;
import com.jz.community.basecomm.bean.LoginInfo;
import com.jz.community.moduleauthorization.login.task.GetBindJupshTask;
import com.jz.community.moduleauthorization.login.task.GetRefreshTokenTask;

/**
 * Created by Tony.Fan on 2018/6/13 13:42
 * 请通过反射暴露出去的方法来完成相应的方法
 */
public class RefreshTokenActionUtils {



    private RefreshTokenActionUtils() {

    }

    /**
     * 跳转到页面
     *
     * @param context
     */
    public static void jumpRefreshToken(Context context) {
        if (!Preconditions.isNullOrEmpty(BaseSpUtils.getInstance().getRefreshToken(context)) && BaseSpUtils.getInstance().getIsLogin(context)) {
            BaseSpUtils.getInstance().setIsLogin(context, false);
            new GetRefreshTokenTask(BaseApplication.getAppInstance(), o -> {
                LoginInfo loginInfo = (LoginInfo) o;
                if (!Preconditions.isNullOrEmpty(loginInfo)) {
                    if (!Preconditions.isNullOrEmpty(loginInfo.getAccess_token())) {
                        BaseSpUtils.getInstance().setAccessToken(context, loginInfo.getAccess_token());
                        BaseSpUtils.getInstance().setIsLogin(context, true);
                    } else {
                        setRefreshTokenState(context);
                    }
                } else {
                    setRefreshTokenState(context);
                }
            }).execute();
        }
    }

    /**
     * 设置RefreshToken状态
     *
     */
    private static void setRefreshTokenState(Context context) {
        RouterCommonUtils.startNoParameterActivity((Activity) context,RouterIntentConstant.MODULE_AUTHORIZATION_LOGIN, RequseResultConstants.REFESH_TOKEN_CODE);
        ToLoginEvent toLoginEvent = new ToLoginEvent();
        toLoginEvent.setToPwdLogin(false);
        RxBus.getInstance().post(toLoginEvent);
    }

    /**
     * 跳转到页面
     *
     * @param context
     */
    public static void jumpBindPush(Context context, String jpushid) {
        new GetBindJupshTask((Activity) context, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {

            }
        }).execute(jpushid);
    }
}
